import wxcharts from '../../API/wxcharts.js' //引入
Page({
  data: {
    show: false,
    list: 0,
    dqweight: 0,
    weight: 0,
    BMI: 0,
    height: 0,
    heights: 0,
    heightscharge: [0, 0, 0, 0, 0],
    timescharge: ['0000-00-00', '0000-00-00', '0000-00-00', '0000-00-00', '0000-00-00'],
    autoBMI: 'BMI:自动计算',
    //data中配置
    ec: {
      lazyLoad: true
    }
  },
  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },
  onClick: (event) => {
    let index = event.detail.index
    let url = ['weight', 'BMI', 'height']
    wx.redirectTo({
      url: `/pages/${url[index]}/${url[index]}`,
    })
    // console.log(title)
  },
  postdata: function (e) {
    var app = getApp()
    var ip = app.globalData.ServerAddress
    let height = e.detail.value.height
    let weight = e.detail.value.weight
    let that = this
    console.log(height)
    console.log(weight)
    wx.request({
      url: "http://" + ip + "/user/posture_recording",
      method: 'PUT',
      dataType: "json",
      header: {
        'Content-type': 'application/json',
        "authorization": app.globalData.token
      },
      data: {
        "user_id": app.globalData.user_id,
        "weight": weight,
        "height": height
      },
      success: function (res) {
        console.log("成功！")
        // const pages = getCurrentPages()     //获取页面列表
        // const perpage = pages[pages.length - 1]    //当前页   
        // perpage.onLoad() 
        that.onLoad()
      }
    })
  },
  onLoad() {

    var app = getApp()
    var ip = app.globalData.ServerAddress
    console.log(ip)
    let that = this
    wx.request({
      url: "http://" + ip + "/user/posture_recording",
      method: 'POST',
      dataType: "json",
      header: {
        'Content-type': 'application/json',
        "authorization": app.globalData.token
      },
      data: {
        user_id: app.globalData.user_id
      },
      success: function (res) {

        let ll = res.data.PostureRecording
        console.log(ll)
        if (ll.length > 0) {
          that.setData({
            // list: res.data.PostureRecording.length,
            heights: res.data.PostureRecording,
            weight: res.data.PostureRecording[0].weight,
            height: res.data.PostureRecording[0].height,
            BMI: (res.data.PostureRecording[0].weight / (res.data.PostureRecording[0].height * res.data.PostureRecording[0].height * 0.0001)).toFixed(1),
            dqweight: res.data.PostureRecording[0].weight,
          })
          let arr = []
          for (let i = ll.length - 1; i >= 0; i--) {
            that.data.heightscharge.push(ll[i].weight)
            console.log(that.data.heightscharge)
            that.data.timescharge.push(ll[i].time.slice(0, 10))
            
          }
          console.log(that.data.timescharge)
          new wxcharts({
            canvasId: 'lineCanvas',
            extra: {
              lineStyle: 'curve'
            },
            type: 'line',
            categories: [that.data.timescharge[that.data.timescharge.length - 5], that.data.timescharge[that.data.timescharge.length - 4], that.data.timescharge[that.data.timescharge.length - 3], that.data.timescharge[that.data.timescharge.length - 2], that.data.timescharge[that.data.timescharge.length - 1]],
            series: [{
                name: '体重',
                data: [that.data.heightscharge[that.data.heightscharge.length - 5], that.data.heightscharge[that.data.heightscharge.length - 4], that.data.heightscharge[that.data.heightscharge.length - 3], that.data.heightscharge[that.data.heightscharge.length - 2], that.data.heightscharge[that.data.heightscharge.length - 1]],
                format: function (val) {
                  return val.toFixed(1);
                }
              }

            ],
            yAxis: {
              // title: '体重变化 (公斤)',
              format: function (val) {
                return val.toFixed(2);
              },
              min: 0
            },
            width: 380,
            height: 300,
          });
        }
      }
    })
    console.log(that.data.heightscharge)

  }
})
//that.data.bmischarge[that.data.bmischarge.length - 1], that.data.bmischarge[that.data.bmischarge.length - 2], that.data.bmischarge[that.data.bmischarge.length - 3], that.data.bmischarge[that.data.bmischarge.length - 4], that.data.bmischarge[that.data.bmischarge.length - 5]
//that.data.timescharge[that.data.timescharge.length - 1], that.data.timescharge[that.data.timescharge.length - 2], that.data.timescharge[that.data.timescharge.length - 3], that.data.timescharge[that.data.timescharge.length - 4], that.data.timescharge[that.data.timescharge.length - 5]